package com.apes.framework.plugin.outbox;

import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = OutboxEventBackup.CONST_TABLE_NAME, indexes = {
        @Index(columnList = "outbox_event_id", unique = true),
        @Index(columnList = "timestamp"),
        @Index(columnList = "aggregate_id")})
public class OutboxEventBackup implements ApesBean {
    public static final String CONST_TABLE_NAME = "outbox_event_backup";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "发件箱事件备份表")
    @Comment("消息编号")
    private String id;

    @Column(name = "aggregate_id", length = 500)
    @Comment("聚合根ID")
    private String aggregateId; //受给定事件影响的聚合根的id

    @Column(name = "aggregate_type", length = 128)
    @Comment("聚合根类型")
    private String aggregateType; //与给定事件相关的聚合根的类型

    @Column(name = "event_type", length = 128)
    @Comment("事件类型")
    private String eventType; //类型

    @Column(name = "timestamp", length = 128)
    @Comment("时间戳")
    private Long timestamp; //时间戳

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "payload", columnDefinition = "CLOB")
    @Comment("消息体")
    private String payload;

    @Column(name = "outbox_subscriber_id", length = 4000)
    @Comment("事件订阅者")
    private String subscriber;

    @Column(name = "create_date", updatable = false)
    @Comment("创建时间")
    private Date createDate;

    @Column(name = "update_date", insertable = false)
    @Comment("修改日期")
    private Date updateDate;

    @Column(name = "state")
    @Comment("状态：0-未消费；1-报错；2-已消费")
    private Integer state = 0;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "error", columnDefinition = "CLOB")
    @Comment("错误消息")
    private String error;

    @Column(name = "times")
    @Comment("消费次数")
    private int times = 0; //消费次数

    @Column(name = "session_id", length = 60)
    @Comment("会话ID")
    private String sessionId;

    @Column(name = "handled", columnDefinition = "NUMBER(10)")
    @Comment("遗漏消息处理标记：0-未处理；1-已处理")
    private Integer handled;

    @Column(name = "outbox_event_id", length = 32)
    @Comment("消息编号")
    private String outboxEventId;

}
